(function () {
    'use strict';

    angular.module('KingAdmin.pages.sys.dict')
        .factory('DictService', DictService);

    /** @ngInject */
    function DictService($resource, $filter, toastr, CommonService) {

        var rest = $resource('sys/dict/:id', {}, {
            'create': {method: 'POST'},
            'update': {method: 'PUT'},
        });

        function getSmartData(param, callback) {
            $resource('sys/dict/getSmartData', {}, {
                'query': {method: 'POST'}
            }).query(param,
                function (data) {
                    console.log(data);
                    callback(data)
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }

        function del(param, callback) {
            CommonService.danger('确定删除?', function () {
                rest.delete(param,
                    function (data) {
                        if (data.code == 0) {
                            toastr.success("删除成功！", "提示", {"progressBar": true,});
                            // 更新系统字典
                            updDictMap();
                        } else {
                            toastr.warning("删除失败！", "提示", {"progressBar": true,});
                        }
                        callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    })
            })
        }

        function save(param, callback) {
            CommonService.info('确定保存?', function () {
                if (angular.isDefined(param.id) && param.id != null) {
                    rest.update(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功", "提示", {"progressBar": true,});
                            // 更新系统字典
                            updDictMap();
                            callback(data);
                        }, function (error) {
                            toastr.error(error, "提示", {"progressBar": true,});
                        })
                } else {
                    rest.create(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功", "提示", {"progressBar": true,});
                            // 更新系统字典
                            updDictMap();
                            callback(data);
                        }, function (error) {
                            toastr.error(error, "提示", {"progressBar": true,});
                        })
                }
            });
        }

        function getInfo(param, callback) {
            rest.get(param,
                function (data) {
                    callback(data);
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                })
        }

        function getList(type, callback) {
            $resource('sys/dict/getlist/:type').get({type: type},
                function (data) {
                    callback(data);
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                })
        }

        function getDict(type) {
            $resource('sys/dict/getlist/:type').get({type: type},
                function (data) {
                    console.log(data);
                    return data.result;
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                    return null;
                })
        }

        var dictMap = {
            DEV_ONLINE_STATUS: [],
            DEVICETYPE: [],
            DEVICESTATUS: [],
            DEV_SW_STATUS: [],
            DEV_OPT_STATUS: [],
            DEV_MSG_TYPE: [],
            DEV_OPT_TYPE: [],
            JOB_ENABLE_DICT: [],
            JOB_PAUSE_DICT: [],
        };

        updDictMap();

        function updDictMap() {
            angular.forEach(dictMap, function (value, key) {
                getList(key, function (data) {
                    console.log('get dict key' + key);
                    console.log(data);
                    dictMap[key] = data.result;
                });
            });
        }

        function getDictMap(key) {
            return dictMap[key];
        }


        function getDictLabel(dictKey, value) {
            var selected = [];
            if (dictMap[dictKey] && dictMap[dictKey].length > 0) {
                if (value || value == 0) {
                    selected = $filter('filter')(dictMap[dictKey], {value: value});
                }
                return selected.length ? selected[0].label : '未知';
            }

            return '未找到字典' + dictKey;
        }


        return {
            getSmartData: getSmartData,
            del: del,
            save: save,
            getInfo: getInfo,
            getList: getList,
            getDict: getDict,
            getDictLabel: getDictLabel,
            getDictMap: getDictMap,
        };

    }

})();
